package com.suneast.suneaststore.ui.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.suneast.suneaststore.R;

/**
 * Created by zestz on 2018/3/14.
 */

public class TextIconView extends LinearLayout {
    public TextIconView(Context context) {
        super(context);
        initView(context);
    }

    public TextIconView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.TextIconView);
        resDrawable = mTypedArray.getDrawable(R.styleable.TextIconView_icon_drawable);
        text = mTypedArray.getString(R.styleable.TextIconView_text);
        //获取资源后要及时回收
        mTypedArray.recycle();
        initView(context);
    }

    public TextIconView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private Drawable resDrawable;
    private String text;

    private ImageView iconImgV;
    private TextView textV;

    private void initView(Context ctx) {
        LayoutInflater.from(ctx).inflate(R.layout.layout_text_icon, this, true);
        iconImgV = findViewById(R.id.icon_img);
        textV = findViewById(R.id.text_tv);
        if (resDrawable != null) {
            iconImgV.setImageDrawable(resDrawable);
        }
        textV.setText(text);
    }
}
